﻿@using System.Linq.Expressions;
@using Microsoft.AspNetCore.Authorization;
@using Microsoft.Extensions.Logging
@using ThingsGateway.Admin.Application;
@using ThingsGateway.Admin.Blazor
@using ThingsGateway.Core.Extension
@using ThingsGateway.Core.Extension.Json
@using ThingsGateway.Gateway.Application
@using TouchSocket.Core
@using ThingsGateway.Foundation.TypeExtension

@namespace ThingsGateway.Gateway.Blazor
@inject UserResoures UserResoures
@inherits BaseComponentBase
@* 一个简单的适配网关动态属性的组件，直接判断类型，适用string,数值,enum,dateTime,bool *@
@if (Type.PropertyType != null)
{
    @if (Type.PropertyType == typeof(bool))
    {
        <MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
             <ActivatorContent>
                 <MSelect Class="" Items=@(new List<bool>(){true,false})
                          Value=@Value ValueChanged=@ValueChanged Label=@(Description) ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
                          MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                          ItemText=@((u) =>u.ToString()) @attributes="@tip.Attrs"
                          ItemValue=@(u =>u.ToString())
                          Dense HideDetails=@("auto")
                          Outlined></MSelect>
             </ActivatorContent>
             <ChildContent>
                 <span>@Remark</span>
            </ChildContent>
        </MTooltip>



    }
@*     else if (Type.PropertyType == typeof(DateTime))
    {
                 <PDateTimePicker @bind-Value=Value 
                                  TimeFormat="TimeFormat.Hr24"
                                  ViewType="DateTimePickerViewType.Compact">
                     <ActivatorContent Context="dt">
                         <MTextField Value=@Value ValueChanged=@ValueChanged Label=@Description Required=!Type.IsNullable() Clearable=Type.IsNullable()
                                      ValueExpression="()=>Value"
                                            Filled Outlined
                          HideDetails=@("auto")
                                            Readonly Dense
                                     @attributes="@dt.Attrs">
                         </MTextField>
                     </ActivatorContent>
                     </PDateTimePicker>

    } *@
    else if (Type.PropertyType.IsEnum)
    {
        <MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
             <ActivatorContent>
                 <MSelect Class="" Items=@(Type.PropertyType.GetEnumList())
                          Value=@Value ValueChanged=@ValueChanged Label=@(Description) ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
                          MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                          ItemText=@(u =>u.Description) @attributes="@tip.Attrs"
                          ItemValue=@(u =>u.Name)
                          Dense HideDetails=@("auto")
                          Outlined></MSelect>
             </ActivatorContent>
             <ChildContent>
                 <span>@Remark</span>
            </ChildContent>
        </MTooltip>
    }
    else if (Type.PropertyType.IsInteger())
    {
        <MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
             <ActivatorContent>
                 <MTextField Dense @attributes="@tip.Attrs"
                             Type="number" NumberProps=@(prop => { prop.Step = 1; })
                                Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
                             Value=@Value ValueChanged=@ValueChanged Label="@(Description)" />

             </ActivatorContent>
             <ChildContent>
                 <span>@Remark</span>
            </ChildContent>
        </MTooltip>
    }
    else if (Type.PropertyType.IsDecimal())
    {
        <MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
             <ActivatorContent>
                 <MTextField Dense @attributes="@tip.Attrs"
                             Type="number" NumberProps=@(prop => { prop.Step = 0.1m; })
                                Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
                             Value=@Value ValueChanged=@ValueChanged Label="@(Description)" />

             </ActivatorContent>
             <ChildContent>
                 <span>@Remark</span>
            </ChildContent>
        </MTooltip>
    }
    else
    {
        if (IsBigText)
        {
            <MRow Align="AlignTypes.Center">
                <MCol Md=9 Cols=12>
                    <MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
                         <ActivatorContent>
                             <MTextarea Dense @attributes="@tip.Attrs" AutoGrow
                                          Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
                                        Value=@Value ValueChanged=@ValueChanged Label="@(Description)" />

                         </ActivatorContent>
                         <ChildContent>
                             <span>@Remark</span>
                        </ChildContent>
                    </MTooltip>
                </MCol>
                @if (Type.Name.Contains("BigTextScript"))
                {
                    <MCol Md=3 Cols=12>
                        <MButton Class="mx-2 my-1" OnClick=@(()=>CheckScript()) Color="primary">@AppService.I18n.T("检查")</MButton>
                        </MCol>
                }
            </MRow>
        }
        else
        {
            <MTooltip Disabled=@(string.IsNullOrEmpty(Remark)) Bottom Context="tip">
                 <ActivatorContent>
                     <MTextField Dense @attributes="@tip.Attrs"
                                    Outlined HideDetails="@("auto")" Color="primary" ValueExpression="()=>Value" Required=!Type.IsNullable() Clearable=Type.IsNullable()
                                 Value=@Value ValueChanged=@ValueChanged Label="@(Description)" />

                 </ActivatorContent>
                 <ChildContent>
                     <span>@Remark</span>
                </ChildContent>
            </MTooltip>
        }
    }
}
else
{

}
<PDrawer @bind-Value="DrawerShow" Left
         AutoScrollToTop
          Title=@AppService.I18n.T("检查脚本")
          Width=@(IsMobile?"100%":1000)>
     <MContainer>
         <MSimpleTable Dense FixedHeader>
            <tbody>
                @if (DrawerShow)
                {


                    <tr>
                        <td class="text-start table-minwidth">
                            @AppService.I18n.T("输出")
                        </td>
                        <td class="text-start ">
                            <div style="word-break:break-all; white-space:pre-wrap;">
                                @Output
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td class="text-start table-minwidth">
                            @AppService.I18n.T("输入")
                        </td>
                        <td class="text-start ">
                            <div style="word-break:break-all; white-space:pre-wrap;">
                                @Input
                            </div>
                        </td>
                    </tr>



                    <tr>
                        <td class="text-start table-minwidth">
                            @AppService.I18n.T("脚本")
                        </td>
                        <td class="text-start ">
                            <div style="word-break:break-all; white-space:pre-wrap;">
                                @Value
                            </div>
                        </td>
                    </tr>
                }

            </tbody>
        </MSimpleTable>
    </MContainer>
</PDrawer>

@code {
    private bool DrawerShow;
    private string Input;
    private string Output;
    private void CheckScript()
    {
        try
        {
            if (Type.Name == nameof(BusinessPropertyWithCacheIntervalScript.BigTextScriptAlarmModel))
            {
                List<AlarmVariable> data = new() { new() { 
                    Name = "testName", 
                    DeviceName = "testDevice",
                    AlarmCode = "1", 
                    AlarmTime = DateTime.Now, 
                    EventTime = DateTime.Now, 
                    AlarmLimit = "3", 
                    AlarmType = AlarmTypeEnum.L,
                    EventType=EventTypeEnum.Alarm,
                    Remark1="1",
                    Remark2="2",
                    Remark3="3",
                    Remark4="4",
                    Remark5="5",
                },
                 new() {
                    Name = "testName2",
                    DeviceName = "testDevice",
                    AlarmCode = "1",
                    AlarmTime = DateTime.Now,
                    EventTime = DateTime.Now,
                    AlarmLimit = "3",
                    AlarmType = AlarmTypeEnum.L,
                    EventType=EventTypeEnum.Alarm,
                    Remark1="1",
                    Remark2="2",
                    Remark3="3",
                    Remark4="4",
                    Remark5="5",
                }};
                Input = data.ToJsonString(true);
                var value = data.GetDynamicModel(Value);
                Output = value.ToJsonString(true);
            }
            else if (Type.Name == nameof(BusinessPropertyWithCacheIntervalScript.BigTextScriptVariableModel))
            {
                List<VariableData> data = new() { new() { 
                    Name = "testName", 
                    DeviceName = "testDevice", 
                    Value = "1", 
                    ChangeTime = DateTime.Now, 
                    CollectTime = DateTime.Now,
                    Remark1="1",
                    Remark2="2",
                    Remark3="3",
                    Remark4="4",
                    Remark5="5",
                } ,
                 new() {
                    Name = "testName2",
                    DeviceName = "testDevice",
                    Value = "1",
                    ChangeTime = DateTime.Now,
                    CollectTime = DateTime.Now,
                    Remark1="1",
                    Remark2="2",
                    Remark3="3",
                    Remark4="4",
                    Remark5="5",
                } };
                Input = data.ToJsonString(true);
                var value = data.GetDynamicModel(Value);
                Output = value.ToJsonString(true);
            }
            else if (Type.Name == nameof(BusinessPropertyWithCacheIntervalScript.BigTextScriptDeviceModel))
            {
                List<DeviceData> data = new() { new() {
                    Name = "testDevice",
                    ActiveTime = DateTime.Now,
                    Remark1="1",
                    Remark2="2",
                    Remark3="3",
                    Remark4="4",
                    Remark5="5",
                } ,
                new() {
                    Name = "testDevice2",
                    ActiveTime = DateTime.Now,
                    Remark1="1",
                    Remark2="2",
                    Remark3="3",
                    Remark4="4",
                    Remark5="5",
                }};
                Input = data.ToJsonString(true);
                var value = data.GetDynamicModel(Value);
                Output = value.ToJsonString(true);
            }
        }
        catch (Exception ex)
        {
            Output = ex.ToString();
        }

        DrawerShow = true;
    }

    [Parameter]
    public string? Value { get; set; }

    [Parameter]
    public EventCallback<string?> ValueChanged { get; set; }

    [Parameter]
    public bool IsBigText { get; set; }
    [Parameter]
    public PropertyInfo Type { get; set; }
    [Parameter]
    public string Description { get; set; }
    [Parameter]
    public string Remark { get; set; }
}

